﻿using Microsoft.Extensions.Configuration;
using qcloudsms_csharp;
using qcloudsms_csharp.httpclient;
using qcloudsms_csharp.json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace JinKaiDataBasePlatform.Common.Helper
{
    public class TXSMSHelper
    {

        private static int appid;
        private static string appkey;
        private static string smsSign;

        static TXSMSHelper()
        {

            var build = new ConfigurationBuilder();
            build.SetBasePath(Directory.GetCurrentDirectory());
            build.AddJsonFile("appsettings.json", true, true);
            var appconfig = build.Build();
            var TXSMS = appconfig.GetSection("TXSMS");

            appid = int.Parse(TXSMS["appid"]);
            appkey = TXSMS["appkey"];
            smsSign = TXSMS["smsSign"];
        }
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phoneNumbers">手机号码</param>
        /// <param name="templateId">短信模板id</param>
        ///  <param name="parameters">模板参数</param>
        public void SMS_Send(int templateId, string[] parameters, string phoneNumbers)
        {
            try
            {
                SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
                var result = ssender.sendWithParam("86", phoneNumbers,
                    templateId, parameters, smsSign, "", "");
                Console.WriteLine(result);
            }
            catch (JSONException e)
            {
                Console.WriteLine(e);
            }
            catch (HTTPException e)
            {
                Console.WriteLine(e);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }


        /// <summary>
        /// 群发短信
        /// </summary>
        /// <param name="phoneNumbers">手机号码</param>
        /// <param name="templateId">短信模板id</param>
        ///  <param name="parameters">模板参数</param>
        public void SMS_SendMuti(string[] phoneNumbers, int templateId, params string[] parameters)
        {
            try
            {
                SmsMultiSender msender = new SmsMultiSender(appid, appkey);
                var sresult = msender.sendWithParam("86", phoneNumbers, templateId,
                    parameters, smsSign, "", "");
                Console.WriteLine(sresult);
            }
            catch (JSONException e)
            {
                Console.WriteLine(e);
            }
            catch (HTTPException e)
            {
                Console.WriteLine(e);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

    }
}
